package com.sq.universe.system.controller;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import  com.sq.universe.base.controller.BaseController;
import  com.sq.universe.base.entity.RespEntity;
import  com.sq.universe.system.entity.DepartmentEntity;
import  com.sq.universe.system.service.DepartmentService;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@RequestMapping("/department")
@CrossOrigin
public class DepartmentController extends BaseController {

    @Autowired
    private DepartmentService departmentService;

    /**
    * 查询数据
    * @param departmentEntity
    * @return
    */
    @PostMapping("/query")
    @RequiresPermissions("system:department:query")
    public RespEntity query(@RequestBody(required = false) DepartmentEntity departmentEntity){
        try {
            return new RespEntity("查询成功",departmentService.query(departmentEntity));
        }catch (Exception exception){
            return new RespEntity("查询失败");
        }
    }

    /**
    * 分页查询
    * @param departmentEntity
    * @return
    */
    @PostMapping("/queryByPage")
    @RequiresPermissions("system:department:queryByPage")
    public RespEntity queryByPage(@RequestBody(required = false) DepartmentEntity departmentEntity){
        try {
            PageHelper.startPage(departmentEntity.getPageNo(),departmentEntity.getPageSize());
            List<DepartmentEntity> departmentList = departmentService.query(departmentEntity);
            PageInfo<DepartmentEntity> pageInfo = new PageInfo<DepartmentEntity>(departmentList);
            return new RespEntity("查询成功",pageInfo);
        }catch (Exception exception){
            return new RespEntity("查询失败");
        }
    }

    /**
    * 插入
    * @param departmentEntity
    * @return
    */
    @PostMapping("/insert")
    @RequiresPermissions("system:department:insert")
    public RespEntity insert(@RequestBody(required = false) DepartmentEntity departmentEntity){
        try {
            departmentService.insert(departmentEntity);
            return new RespEntity("保存成功");
        }catch (Exception exception){
            return new RespEntity("保存失败");
        }
    }

    /**
    * 更新
    * @param departmentEntity
    * @return
    */
    @PostMapping("/update")
    @RequiresPermissions("system:department:update")
    public RespEntity update(@RequestBody(required = false) DepartmentEntity departmentEntity){
        try {
            departmentService.update(departmentEntity);
            return new RespEntity("更新成功");
        }catch (Exception exception){
            return new RespEntity("更新失败");
        }
    }

    /**
    * 删除成功
    * @param departmentEntity
    * @return
    */
    @PostMapping("/delete")
    @RequiresPermissions("system:department:delete")
    public RespEntity delete(@RequestBody(required = false) DepartmentEntity departmentEntity){
        try {
            if(departmentEntity.getDepartmentId() == null){
                return new RespEntity("删除失败");
            }
            departmentService.logicalDelete(departmentEntity);
            return new RespEntity("删除成功");
        }catch (Exception exception){
            return new RespEntity("删除失败");
        }
    }

    /**
     * 查询数据
     * @param departmentEntity
     * @return
     */
    @PostMapping("/queryTree")
    @RequiresPermissions("system:department:queryTree")
    public RespEntity queryTree(@RequestBody(required = false) DepartmentEntity departmentEntity){
        try {
            return new RespEntity("查询成功",departmentService.queryTree(departmentEntity));
        }catch (Exception exception){
            return new RespEntity("查询失败");
        }
    }
}